//
//  FrenchCurrencyViewController.m
//  digitalCurrency
//
//  Created by chu on 2018/8/2.
//  Copyright © 2018年 XinHuoKeJi. All rights reserved.
//

#import "CEAppsViewController.h"
#import "SectionHeaderResuView.h"
#import "FlashCell.h"
#import "WMHTMLViewController.h"
#import "HomeNetManager.h"
static NSString *identifier = @"cxCellID";
static CGFloat kMagin = 10.f;

@interface CEAppsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) NSArray * imgs;
@property (nonatomic, strong) NSArray * titles;
@property (nonatomic, strong) UILabel *tips;
//@property (nonatomic, strong) SectionHeaderResuView * header;

@property (nonatomic, strong) UILabel * item1;
@property (nonatomic, strong) UILabel * item2;
@property (nonatomic, strong) UILabel * item3;

@property (nonatomic, strong) NSArray *gameArr;

@end

@implementation CEAppsViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.title = LocalizationKey(@"Application");
    self.tips.text = LocalizationKey(@"Coming soon");
    if (_collectionView) {
        _collectionView = nil;
    }
    [self.view addSubview:self.collectionView];
//    self.item1.text = LocalizationKey(@"Media");
//    self.item2.text = LocalizationKey(@"Game");
//    self.item3.text = LocalizationKey(@"Appicon");
    
    [self getAllCoinInfo];
}

#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.gameArr = [[NSArray alloc]init];
    
    _imgs = @[@"media",@"appicon",@"game"];
    _titles = @[LocalizationKey(@"Media"),LocalizationKey(@"Game"),LocalizationKey(@"Appicon")];
    
    [self.view addSubview:self.collectionView];
}

- (void)getAllCoinInfo {
    
    [HomeNetManager getGameListInfo:[YLUserInfo shareUserInfo].ID CompleteHandle:^(id resPonseObj, int code) {
        if (code) {
            if ([resPonseObj[@"code"] integerValue] == 0) {
                
                self.gameArr = [[NSArray alloc]init];
                self.gameArr = resPonseObj[@"data"];
                [self.collectionView reloadData];
            }
        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

#pragma mark - deleDate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    }
    else if (section == 1) {
        return self.gameArr.count;
    }
    else{
        return 6;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    FlashCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        switch (indexPath.item) {
            case 0:
                cell.imgview.image = [UIImage imageNamed:@"app_icon_c"];
                cell.titlelabel.text = @"BitcoinTalk";
                break;
            case 1:
                cell.imgview.image = [UIImage imageNamed:@"app_icon_b"];
                cell.titlelabel.text = LocalizationKey(@"News.btc");
                break;
            case 2:
                cell.imgview.image = [UIImage imageNamed:@"app_icon_t"];
                cell.titlelabel.text = LocalizationKey(@"8btc");
                break;
            case 3:
                cell.imgview.image = [UIImage imageNamed:@"app_icon_mytoken"];
                cell.titlelabel.text = LocalizationKey(@"MyToken");
                break;
            default:
                break;
        }
    }
    else if (indexPath.section == 1) {
        //game
        NSDictionary *gameDic = self.gameArr[indexPath.item];
        [cell.imgview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMAGE,gameDic[@"picUrl"]]] placeholderImage:[UIImage imageNamed:@"app_icon1"]];
        cell.imgview.layer.cornerRadius = 45.0/2.0;
        cell.imgview.clipsToBounds = YES;
        cell.imgview.contentMode = UIViewContentModeScaleAspectFill;
        cell.titlelabel.text = gameDic[@"name"];
        
    }
    else {
        switch (indexPath.item) {
            case 0:
                cell.imgview.image = [UIImage imageNamed:@"app_icon2"];
                cell.titlelabel.text = LocalizationKey(@"Etherscan");
                break;
            case 1:
                cell.imgview.image = [UIImage imageNamed:@"app_icon3"];
                cell.titlelabel.text = LocalizationKey(@"Btc.com");
                break;
            case 2:
                cell.imgview.image    = [UIImage imageNamed:@"app_icon_coinall"];
                cell.titlelabel.text = LocalizationKey(@"Coinall");
                break;
            case 3:
                cell.imgview.image    = [UIImage imageNamed:@"app_icon_zbg"];
                cell.titlelabel.text = LocalizationKey(@"ZBG");
                break;
            case 4:
                cell.imgview.image    = [UIImage imageNamed:@"app_icon1"];
                cell.titlelabel.text = LocalizationKey(@"Borrowing");
                break;
            default:
                cell.imgview.image   = [UIImage imageNamed:@"app_icon1"];
                cell.titlelabel.text = @"Mall";
                break;
        }
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    FlashCell * cell = (FlashCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    WMHTMLViewController *aboutC = [[WMHTMLViewController alloc] init];
    CGFloat bottomH     = kEasyShowSafeBottomMargin_S;
    aboutC.webViewRect  = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - kTopHeight - bottomH);
    aboutC.canScroll                = YES;
    aboutC.showProgressView         = YES;
    aboutC.hidesBottomBarWhenPushed = YES;
    
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                aboutC.htmlUrl = @"https://bitcointalk.com/";
                break;
            case 1:
                aboutC.htmlUrl = @"https://news.bitcoin.com/";
                break;
            case 2:
                aboutC.htmlUrl = @"https://m.8btc.com/";
                break;
            case 3:
                aboutC.htmlUrl = @"https://www.mytoken.io/";
                break;
            default:
                break;
        }
        aboutC.webViewTitle = cell.titlelabel.text;
        [self.navigationController pushViewController:aboutC animated:YES];
    }
    else if (indexPath.section == 1) {
        
        NSDictionary *gameDic = self.gameArr[indexPath.item];

        aboutC.htmlUrl = [NSString stringWithFormat:@"%@?userId=%@&gameId=%@&baseUrl=%@",gameDic[@"gameUrl"],[YLUserInfo shareUserInfo].ID,gameDic[@"id"],HOST];
        aboutC.webViewTitle = cell.titlelabel.text;
        aboutC.gameIdStr = [NSString stringWithFormat:@"%@",gameDic[@"id"]];
        aboutC.webViewRect  = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        aboutC.isBouncesStr = @"1";
        [self.navigationController pushViewController:aboutC animated:YES];
    }
    else {
        switch (indexPath.row) {
            case 0:
                aboutC.htmlUrl = @"https://etherscan.io/";
                aboutC.webViewTitle = cell.titlelabel.text;
                [self.navigationController pushViewController:aboutC animated:YES];
                break;
            case 1:
                aboutC.htmlUrl = @"https://m.btc.com/";
                aboutC.webViewTitle = cell.titlelabel.text;
                [self.navigationController pushViewController:aboutC animated:YES];
                break;
            case 2:
                aboutC.htmlUrl = @"https://www.coinall.live/";
                aboutC.webViewTitle = cell.titlelabel.text;
                [self.navigationController pushViewController:aboutC animated:YES];
                break;
            case 3:
                aboutC.htmlUrl = @"https://www.zbg.com";
                aboutC.webViewTitle = cell.titlelabel.text;
                [self.navigationController pushViewController:aboutC animated:YES];
                break;
            default:
                [self.view makeToast:LocalizationKey(@"Coming soon") duration:1.5 position:CSToastPositionCenter];
                break;
        }
        
    }
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    //kind有两种 一种时header 一种事footer
    SectionHeaderResuView * header;
    if (kind == UICollectionElementKindSectionHeader) {
        header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        header.backgroundColor = [UIColor blackColor];
        header.titleImgView.image = [UIImage imageNamed:_imgs[indexPath.section]];
        if (indexPath.section == 0) {
            header.titleLabel.text = LocalizationKey(@"Media");
        }
        else if (indexPath.section == 1){
            header.titleLabel.text = LocalizationKey(@"Game");
        }
        else {
            header.titleLabel.text = LocalizationKey(@"Appicon");
        }
        
        return header;
    }
    
    
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(self.view.frame.size.width, 68);
}

#pragma mark - set_and_get
-(UICollectionView *)collectionView {
    if (!_collectionView) {
        //自动网格布局
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
        CGFloat spaceWidth = (SCREEN_WIDTH - 4 * 45) / 5;
        //设置单元格大小
        flowLayout.itemSize = CGSizeMake(45, 45);
        //最小行间距(默认为10)
        flowLayout.minimumLineSpacing = 40;
        //最小item间距（默认为10）
        flowLayout.minimumInteritemSpacing = spaceWidth;
        //设置senction的内边距
        flowLayout.sectionInset = UIEdgeInsetsMake(kMagin, spaceWidth, spaceWidth, spaceWidth);
        //sectionHeader的大小,如果是竖向滚动，只需设置Y值。如果是横向，只需设置X值。
        //flowLayout.headerReferenceSize = CGSizeMake(100,0);
        //网格布局
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor blackColor];
        
        [_collectionView registerClass:[FlashCell class] forCellWithReuseIdentifier:identifier];
        //[_collectionView registerClass:[FlashNonCell class] forCellWithReuseIdentifier:@"noncell"];
        
        //注册header
        [_collectionView registerClass:[SectionHeaderResuView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        //设置数据源代理
        _collectionView.dataSource = self;
        _collectionView.delegate   = self;
    }
    
    return _collectionView;
}

@end
